package com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPlanningPt.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPlanningPt.repository.HddcWyGeologicalsvyplanningptNativeRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPlanningPt.repository.HddcWyGeologicalsvyplanningptRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPlanningPt.repository.entity.HddcWyGeologicalsvyplanningptEntity;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPlanningPt.service.HddcWyGeologicalsvyplanningptService;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPlanningPt.viewobjects.HddcWyGeologicalsvyplanningptQueryParams;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyPlanningPt.viewobjects.HddcWyGeologicalsvyplanningptVO;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zyb
 * @date 2020-11-30
 */
@Service
public class HddcWyGeologicalsvyplanningptServiceImpl implements HddcWyGeologicalsvyplanningptService {

	@Autowired
    private HddcWyGeologicalsvyplanningptRepository hddcWyGeologicalsvyplanningptRepository;
    @Autowired
    private HddcWyGeologicalsvyplanningptNativeRepository hddcWyGeologicalsvyplanningptNativeRepository;

    @Override
    public JSONObject queryHddcWyGeologicalsvyplanningpts(HddcWyGeologicalsvyplanningptQueryParams queryParams ,
                                                          int curPage, int pageSize) {
        Page<HddcWyGeologicalsvyplanningptEntity> hddcWyGeologicalsvyplanningptPage =
                this.hddcWyGeologicalsvyplanningptNativeRepository.queryHddcWyGeologicalsvyplanningpts(queryParams , curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcWyGeologicalsvyplanningptPage);
        return jsonObject;
    }


    @Override
    public HddcWyGeologicalsvyplanningptEntity getHddcWyGeologicalsvyplanningpt(String id) {
        HddcWyGeologicalsvyplanningptEntity hddcWyGeologicalsvyplanningpt = this.hddcWyGeologicalsvyplanningptRepository.findById(id).orElse(null);
         return hddcWyGeologicalsvyplanningpt;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeologicalsvyplanningptEntity saveHddcWyGeologicalsvyplanningpt(HddcWyGeologicalsvyplanningptEntity hddcWyGeologicalsvyplanningpt) {
        String uuid = UUIDGenerator.getUUID();
        hddcWyGeologicalsvyplanningpt.setId(uuid);
        hddcWyGeologicalsvyplanningpt.setCreateUser(PlatformSessionUtils.getUserId());
        hddcWyGeologicalsvyplanningpt.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        hddcWyGeologicalsvyplanningpt.setIsValid("1");
        this.hddcWyGeologicalsvyplanningptRepository.save(hddcWyGeologicalsvyplanningpt);
        return hddcWyGeologicalsvyplanningpt;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeologicalsvyplanningptEntity updateHddcWyGeologicalsvyplanningpt(HddcWyGeologicalsvyplanningptEntity hddcWyGeologicalsvyplanningpt) {
        hddcWyGeologicalsvyplanningpt.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcWyGeologicalsvyplanningpt.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyGeologicalsvyplanningptRepository.save(hddcWyGeologicalsvyplanningpt);
        return hddcWyGeologicalsvyplanningpt;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcWyGeologicalsvyplanningpts(List<String> ids) {
        List<HddcWyGeologicalsvyplanningptEntity> hddcWyGeologicalsvyplanningptList = this.hddcWyGeologicalsvyplanningptRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcWyGeologicalsvyplanningptList) && hddcWyGeologicalsvyplanningptList.size() > 0) {
            for(HddcWyGeologicalsvyplanningptEntity hddcWyGeologicalsvyplanningpt : hddcWyGeologicalsvyplanningptList) {
//                this.hddcWyGeologicalsvyplanningptRepository.delete(hddcWyGeologicalsvyplanningpt);
                hddcWyGeologicalsvyplanningpt.setIsValid("0");
                hddcWyGeologicalsvyplanningpt.setUpdateUser(PlatformSessionUtils.getUserId());
                hddcWyGeologicalsvyplanningpt.setUpdateTime(new Date());
                this.hddcWyGeologicalsvyplanningptRepository.save(hddcWyGeologicalsvyplanningpt);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

    @Override
    public BigInteger queryHddcWyGeologicalsvyplanningpt(HddcWyGeologicalsvyplanningptQueryParams queryParams) {
        BigInteger bigInteger = hddcWyGeologicalsvyplanningptNativeRepository.queryHddcWyGeologicalsvyplanningpt(queryParams);
        return bigInteger;
    }


}
